/*
	Simple Software Renderer (SSRenderer)
	Copyright (C) 2007 Piotr Orzechowski <piotr.orzechowski@gmail.com>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef _SSRSURFACE_H
#define _SSRSURFACE_H

#include <memory>
#include <assert.h>
#include "./SSRTypes.h"
#include "./ISSRObject.h"

namespace ssr
{

/**
	SSRSurface is a class representing a color buffer.

	For the sake of simplicity, we assume that every buffer
	contains rectangular portion of 32-bit values. Each color
	in the buffer is stored in 0xAARRGGBB fashion.
*/
class SSRSurface :
	public ISSRObject
{
public:

	/**
		Static method used to create an instance of SSRSurface.

		Important: CreateSurface() is the only way of creating
		an instace of SSRSurface.

		When the object is no longer needed, just call
		SSR_RELEASE(ptr) with a pointer to	the object.

		@param width width of the surface
		@param height height of the surface

		@return pointer to the created surface
	*/
	static SSRSurface* CreateSurface(u32 width, u32 height);

	/**
		Static method used to create an instance of SSRSurface
		basing on an existing SSRSurface.

		Important: CreateSurface() is the only way of creating
		an instace of SSRSurface.

		When the object is no longer needed, just call
		SSR_RELEASE(ptr) with a pointer to	the object.

		@param source source SSRSurface

		@return pointer to the created surface
	*/
	static SSRSurface* CreateSurface(const SSRSurface &source);

	/**
		Returns a pointer to the buffer.

		Important : someone who attempts to delete the returned
		pointer is an *idiot*. I know that I could try to avoid
		such situation but it would clearly cause a performance hit
		(which I don't want).

		@return pointer to the buffer
	*/
	color32* GetBuffer()
	{
		assert(m_pBuffer != NULL && "SSRSurface : we've got an idiot!");
		return m_pBuffer;
	}

	/**
		Returns width of the surface.

		@return width of the surface
	*/
	u32 GetWidth() const
	{
		return m_Width;
	}

	/**
		Returns height of the surface.

		@return height of the surface
	*/
	u32 GetHeight() const
	{
		return m_Height;
	}

	/**
		Returns length of a surface scanline in color32 structures.

		@return length of a surface scanline in color32 structures
	*/
	u32 GetPitch() const
	{
		return m_Pitch;
	}

	/**
		Returns NULL-ended string containing name of the class.
		Satisfies ISSRObject implementation.

		@return NULL-ended string containing name of the class
	*/
	const u8* ClassName()
	{
		const static u8 name[] = "SSRSurface\0";
		return name;
	}

protected:

	/**
		Default constructor.
	*/
	SSRSurface()
	{
		m_pBuffer = NULL;
		m_Width = m_Height = m_Pitch = 0;
	}

	/**
		Destructor.
	*/
	~SSRSurface()
	{
		delete [] m_pBuffer;
	}

private:

	u32 m_Width;
	u32 m_Height;
	u32 m_Pitch;

	color32 *m_pBuffer;
};

} // namespace

#endif
